package com.wangsd.web.mqpay.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.mqpay.entity.Trade;
import com.wangsd.web.mqpay.service.TradeService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "交易管理")
@RestController
@RequestMapping("/mqpay/trade")
@Slf4j
public class TradeController extends MyController {
    @Autowired
    private TradeService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Trade queryObj) {
        log.debug("分页查询列表-controller");
        if (StringUtils.isNotBlank(queryObj.getEndTime())) {
            queryObj.setEndTime(queryObj.getEndTime() + " 23:59:59");
        }
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Trade> list = service.selectPage(queryObj, loginUser.getCode());
        PageInfo pageInfo = getPageInfo(page, list);

        //获取当前条件的订单信息统计
        Map statistics = service.selectStatistics(queryObj, loginUser.getCode());
        Map result = new HashMap();
        result.put("pageInfo", pageInfo);
        result.put("statistics", statistics);
        return Result.success(result);
    }


    @ApiOperation(value = "从新通知")
    @PostMapping("/doNotice")
    public Result doNotice(@ApiParam(value = "商户订单号", required = true) @RequestParam String outTradeNo) {
        log.debug("从新通知-controller");
        Trade trade = service.selectByOutTradeNo(outTradeNo);
        boolean isFlag = service.doNotice(trade, null);
        return Result.success(200, isFlag ? "通知成功" : "通知失败");
    }
}
